﻿

<!--#include file=md5.asp-->

<%
	Dim dbPath	: dbPath = Server.MapPath("db.mdb")
	Dim strCon 	: strCon = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & dbPath & "; Jet OLEDB:Database Password=*-793Yi;"
	Dim con
	Dim action
	
	action = Request.Form("action")
	openCon
	
	Select Case action
		Case 1
			login

		Case 3
			carregaTiposDesp
			
		Case 4
			carregaDespesas
		
		Case 5
			cadastraDespesa
		
	End Select
	
	closeCon
	
	'==============================================================================
	Sub cadastraDespesa
		On Error Resume Next
		
		Dim sql  	: sql = ""
		Dim retorno	: retorno = ""
		Dim rs
		Set rs = Server.CreateObject("ADODB.Recordset")
		Dim desc	: desc = Request.Form("desc")
		Dim valor	: valor = Request.Form("valor")
		Dim dtvenc	: dtvenc = Request.Form("dtvenc")
		Dim dtpag	: dtpag = Request.Form("dtpag")
		Dim ativo	: ativo = Request.Form("ativo")
		Dim obs		: obs = Request.Form("obs")
		Dim tipo	: tipo = Request.Form("tipo")
		
		sql = ""
		sql = "INSERT INTO DESPESAS (DESCR, DTVENCIMENTO, DTPAGAMENTO, VALOR, OBS, TIPO, ATIVO)VALUES('"
		sql = sql & desc & "',#" & dtvenc & "#,#" & dtpag & "#," & valor & ","
		sql = sql & IIF(Trim(obs) = "", "NULL", "'" & obs & "'") & ","
		sql = sql & tipo & "," & ativo & ")"
		
		con.Execute sql
		Set rs = Nothing
		
		If err.number <> 0 Then
			Response.Write "<tr><td colspan='8'>" & err.Description & "</td></tr>"
		End If
	End Sub
	
	'==============================================================================
	Sub carregaDespesas
		On Error Resume Next
		
		Dim sql  	: sql = ""
		Dim retorno	: retorno = ""
		Dim rs
		Set rs = Server.CreateObject("ADODB.Recordset")
		
		sql = "SELECT D.ID AS ID, D.DESCR AS DESCR, D.VALOR AS VALOR, D.DTVENCIMENTO AS VENCIMENTO, "
		sql = sql & "D.DTPAGAMENTO AS PAGAMENTO, TIP.DESCR AS DESCTIP, D.ATIVO AS ATIVO, "
		sql = sql & "D.OBS AS OBS FROM DESPESAS D "
		sql = sql & "INNER JOIN TIPODESP TIP ON D.TIPO = TIP.ID "
		
		rs.Open sql, con
		
		If Not rs.EoF Then
			Do While Not rs.EoF
				retorno = retorno & "<tr>"
				retorno = retorno & "<td style='display:none'>" & rs("ID") & "</td>"
				retorno = retorno & "<td>" & rs("DESCR") & "</td>"
				retorno = retorno & "<td>" & FormatCurrency(rs("VALOR")) & "</td>"
				retorno = retorno & "<td>" & rs("VENCIMENTO") & "</td>"
				retorno = retorno & "<td>" & rs("PAGAMENTO") & "</td>"
				retorno = retorno & "<td>" & rs("DESCTIP") & "</td>"
				retorno = retorno & "<td>" & IIF(rs("ATIVO") = "1", "Sim", "Não") & "</td>"
				retorno = retorno & "<td>" & rs("OBS") & "</td>"
				retorno = retorno & "</tr>" 
				rs.MoveNext
			Loop
		Else
			retorno = "<tr><td colspan='8'>Nenhuma despesa cadastrada</td></tr>"
		End If
		
		Response.Write retorno
		Set rs = Nothing
		
		If err.number <> 0 Then
			Response.Write "<tr><td colspan='7'>" & err.Description & "</td></tr>"
		End If		
	End Sub
	
	'==============================================================================
	Sub carregaTiposDesp
		On Error Resume Next
		
		Dim sql  	: sql = ""
		Dim retorno	: retorno = ""
		Dim rs
		Set rs = Server.CreateObject("ADODB.Recordset")
		
		sql = "SELECT * FROM TIPODESP"
		rs.Open sql, con', adOpenForwardOnly, adLockReadOnly 

		If Not rs.EoF Then			
			Do While Not rs.EoF
				retorno = retorno & "<option value='" & rs(0) & "'>" & rs(1) & "</option>"
				rs.MoveNext
			Loop
		End If
		
		response.write retorno
		Set rs = Nothing
		
		If err.number <> 0 Then
			Response.Write err.Description
		End If
	End Sub
	
	'==============================================================================		
	Sub login
		On Error Resume Next
		
		Dim sql  	: sql = ""
		Dim user 	: user = Request.Form("login")
		Dim pass 	: pass = Request.Form("pass")
		Dim rs
		Set rs = Server.CreateObject("ADODB.Recordset")
		
		sql = "SELECT * FROM USUARIOS WHERE LOGIN = '" & user & "' AND SENHA = '" &  md5(pass) & "'"
		rs.Open sql, con', adOpenForwardOnly, adLockReadOnly

		If Not rs.EoF Then
			Session("user") = rs(1)
			Response.Write "menu.asp"
		Else
			Response.Write "-1Usuário e senha não conferem."
		End If

		Set rs = Nothing
		If err.number <> 0 Then
			Response.Write err.Description
		End If
	End Sub
	
	'==============================================================================	
	Function IIF(condicao, verd, fals)
		If condicao Then
			IIF = verd
		Else
			IIF = fals
		End If	
	End Function

	'==============================================================================	
	Sub openCon
		Set con = Server.CreateObject("ADODB.Connection")
		con.Mode = 3
		con.Open strCon
	End Sub	
	
	'==============================================================================
	Sub closeCon
		con.Close
		Set con = Nothing
	End Sub
%>